{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ad553e51",
   "metadata": {},
   "source": [
    "# Async Chromium\n",
    "\n",
    "Chromium is one of the browsers supported by Playwright, a library used to control browser automation. \n",
    "\n",
    "By running `p.chromium.launch(headless=True)`, we are launching a headless instance of Chromium. \n",
    "\n",
    "Headless mode means that the browser is running without a graphical user interface.\n",
    "\n",
    "In the below example we'll use the `AsyncChromiumLoader` to loads the page, and then the [`Html2TextTransformer`](/docs/integrations/document_transformers/html2text/) to strip out the HTML tags and other semantic information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c3a4c19",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install --upgrade --quiet playwright beautifulsoup4 html2text\n",
    "!playwright install"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00487c0f",
   "metadata": {},
   "source": [
    "**Note:** If you are using Jupyter notebooks, you might also need to install and apply `nest_asyncio` before loading the documents like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d374eef4",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install nest-asyncio\n",
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd2cdea7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<!DOCTYPE html><html lang=\"en\" dir=\"ltr\" class=\"docs-wrapper docs-doc-page docs-version-2.0 plugin-d'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_community.document_loaders import AsyncChromiumLoader\n",
    "\n",
    "urls = [\"https://docs.smith.langchain.com/\"]\n",
    "loader = AsyncChromiumLoader(urls, user_agent=\"MyAppUserAgent\")\n",
    "docs = loader.load()\n",
    "docs[0].page_content[0:100]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7eb5e6aa",
   "metadata": {},
   "source": [
    "Now let's transform the documents into a more readable syntax using the transformer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "013caa7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Skip to main content\\n\\nGo to API Docs\\n\\nSearch`⌘``K`\\n\\nGo to App\\n\\n  * Quick start\\n  * Tutorials\\n\\n  * How-to guides\\n\\n  * Concepts\\n\\n  * Reference\\n\\n  * Pricing\\n  * Self-hosting\\n\\n  * LangGraph Cloud\\n\\n  *   * Quick start\\n\\nOn this page\\n\\n# Get started with LangSmith\\n\\n**LangSmith** is a platform for building production-grade LLM applications. It\\nallows you to closely monitor and evaluate your application, so you can ship\\nquickly and with confidence. Use of LangChain is not necessary - LangSmith\\nworks on it'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_community.document_transformers import Html2TextTransformer\n",
    "\n",
    "html2text = Html2TextTransformer()\n",
    "docs_transformed = html2text.transform_documents(docs)\n",
    "docs_transformed[0].page_content[0:500]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
